cmake_minimum_required(VERSION 3.15)

# 设置工具链文件
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/arm-none-eabi-gcc.cmake)

# 项目名称
project(nwatch_game_freertos C ASM)

# MCU定义
add_definitions(
    -DUSE_HAL_DRIVER
    -DSTM32F103xB
)

# 包含目录
include_directories(
    .
    Core/Inc
    Drivers/STM32F1xx_HAL_Driver/Inc/Legacy
    Drivers/STM32F1xx_HAL_Driver/Inc
    Middlewares/Third_Party/FreeRTOS/Source/include
    Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2
    Drivers/CMSIS/Device/ST/STM32F1xx/Include
    MDK-ARM/RTE/_01_freertos_template
    Drivers/CMSIS/Include
    Drivers/DshanMCU-F103
    .cmsis/include
    nwatch
    Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3
)

# Core源文件
set(CORE_SOURCES
    "Core/Src/main.c"
    "Core/Src/gpio.c"
    "Core/Src/freertos.c"
    "Core/Src/adc.c"
    "Core/Src/i2c.c"
    "Core/Src/spi.c"
    "Core/Src/tim.c"
    "Core/Src/usart.c"
    "Core/Src/usb.c"
    "Core/Src/stm32f1xx_it.c"
    "Core/Src/stm32f1xx_hal_msp.c"
    "Core/Src/stm32f1xx_hal_timebase_tim.c"
    "Core/Src/system_stm32f1xx.c"
    "Core/Src/music.c"
)

# HAL驱动源文件
set(HAL_SOURCES
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc_ex.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pcd.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pcd_ex.c"
    "Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_usb.c"
)

# DshanMCU-F103驱动源文件
set(DSHAN_SOURCES
    "Drivers/DshanMCU-F103/driver_led.c"
    "Drivers/DshanMCU-F103/driver_oled.c"
    "Drivers/DshanMCU-F103/driver_mpu6050.c"
    "Drivers/DshanMCU-F103/driver_timer.c"
    "Drivers/DshanMCU-F103/driver_ds18b20.c"
    "Drivers/DshanMCU-F103/driver_lcd.c"
    "Drivers/DshanMCU-F103/driver_dht11.c"
    "Drivers/DshanMCU-F103/driver_active_buzzer.c"
    "Drivers/DshanMCU-F103/driver_passive_buzzer.c"
    "Drivers/DshanMCU-F103/driver_color_led.c"
    "Drivers/DshanMCU-F103/driver_ir_receiver.c"
    "Drivers/DshanMCU-F103/driver_ir_sender.c"
    "Drivers/DshanMCU-F103/driver_light_sensor.c"
    "Drivers/DshanMCU-F103/driver_ir_obstacle.c"
    "Drivers/DshanMCU-F103/driver_ultrasonic_sr04.c"
    "Drivers/DshanMCU-F103/driver_spiflash_w25q64.c"
    "Drivers/DshanMCU-F103/driver_rotary_encoder.c"
    "Drivers/DshanMCU-F103/driver_irq.c"
    "Drivers/DshanMCU-F103/driver_motor.c"
    "Drivers/DshanMCU-F103/driver_key.c"
    "Drivers/DshanMCU-F103/driver_uart.c"
)

# FreeRTOS源文件
set(FREERTOS_SOURCES
    "Middlewares/Third_Party/FreeRTOS/Source/croutine.c"
    "Middlewares/Third_Party/FreeRTOS/Source/event_groups.c"
    "Middlewares/Third_Party/FreeRTOS/Source/list.c"
    "Middlewares/Third_Party/FreeRTOS/Source/queue.c"
    "Middlewares/Third_Party/FreeRTOS/Source/stream_buffer.c"
    "Middlewares/Third_Party/FreeRTOS/Source/tasks.c"
    "Middlewares/Third_Party/FreeRTOS/Source/timers.c"
    "Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/cmsis_os2.c"
    "Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_4.c"
    "Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM3/port.c"
)

# nwatch源文件
set(NWATCH_SOURCES
    "nwatch/draw.c"
    "nwatch/game1.c"
    "nwatch/resources.c"
)

# 启动文件
set(STARTUP_ASM
    "GCC-ARM/gcc_ride7/startup_stm32f10x_md.s"
)

# 合并所有源文件
set(SOURCES
    ${CORE_SOURCES}
    ${HAL_SOURCES}
    ${DSHAN_SOURCES}
    ${FREERTOS_SOURCES}
    ${NWATCH_SOURCES}
    ${STARTUP_ASM}
)

# 链接脚本
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/GCC-ARM/stm32_flash.ld)


# 可执行文件
add_executable(${PROJECT_NAME}.elf ${SOURCES})
# 确保目标名称一致
target_link_libraries(${PROJECT_NAME}.elf m)

# 链接选项
target_link_options(${PROJECT_NAME}.elf PRIVATE
    -T${LINKER_SCRIPT}
    -Wl,-Map=${PROJECT_NAME}.map
)

# 生成hex和bin文件
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
    COMMAND ${CMAKE_OBJCOPY} -O binary ${PROJECT_NAME}.elf ${PROJECT_NAME}.bin
)

# 显示大小信息
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
    COMMAND ${SIZE} ${PROJECT_NAME}.elf
) 